#!/usr/bin/env bash

function download() {
  local url="${1}"
  local path="${2}"
  curl -f --progress-bar -continue-at=- --location --output "${path}" "${url}"
}

function installdeps() {
  # shellcheck source=/dev/null
  source /etc/os-release
  if [[ "$ID" == "ubuntu" ]]; then
    sudo apt-get install -y make libcurl4-gnutls-dev libexpat1-dev gettext libz-dev curl autoconf perl
  fi
  if [[ "$ID" == "debain" ]]; then
    #statements
    sudo apt-get install -y make libcurl4-gnutls-dev libexpat1-dev gettext libz-dev curl autoconf perl
  fi
  if [[ "$ID" == "centos" ]]; then
    #statements
    sudo yum install -y make curl-devel expat-devel gettext-devel openssl-devel zlib-devel gcc autoconf
  fi
}

function checkdeps() {
  echo "checking dependencies ..."
  if [[ ! -f "/usr/lib/x86_64-linux-gnu/libcurl.a" ]]; then
    #mybe not compile git
    installdeps
  fi
  if ! hash make; then
    installdeps
  fi
}

function compilegit() {
  local sourcedir="${1}"
  local installdir="${2}"
  cd "$sourcedir" || exit 1
  echo "git sources: $sourcedir"
  make configure
  ./configure --prefix="${installdir}"
  if ! make; then
    echo "unable compile"
    exit 1
  fi
  sudo make install
}

checkdeps
readonly GIT_URL_BASE="https://github.com/git/git/archive"
readonly GIT_REMOTE_VERSION="https://gitee.com/oscstudio/git-dist/raw/master/version"
# URL: https://github.com/git/git/archive/$version.tar.gz
Xdirname=$(dirname "$0")
PrefixDir=$(
  cd "$Xdirname"
  pwd
) || exit 1
if [[ -f "${PrefixDir}/version" ]]; then
  # shellcheck source=/dev/null
  source "${PrefixDir}/version"
else
  echo "fetch remote version"
  curl -f --progress-bar --location --output /tmp/git-real-version "$GIT_REMOTE_VERSION"
  if [[ ! -f "/tmp/git-real-version" ]]; then
    echo -e "\\x1b[31munable download git version file\\x1b[0m"
    exit 1
  fi
  # shellcheck source=/dev/null
  source "/tmp/git-real-version"
fi

git_version="$GIT_REAL_VERSION"
echo "GIT_REAL_VERSION: $GIT_REAL_VERSION"

if [[ ! -d "/tmp/buildgit" ]]; then
  #statements
  mkdir -p "/tmp/buildgit"
fi

# https://github.com/git/git/archive/v2.19.0.tar.gz
GIT_TAR_URL="${GIT_URL_BASE}/v${git_version}.tar.gz"
GIT_TAR_PATH="/tmp/buildgit/git-${git_version}.tar.gz"
#https://github.com/git/git/archive/v2.13.2.tar.gz
echo "download: ${GIT_TAR_URL}"

# check sha256sum if GIT_GITHUB_HASH is matched
if [[ -f "$GIT_TAR_PATH" ]]; then
  if [[ "x$GIT_GITHUB_HASH" != "x" ]]; then
    echo "check $GIT_TAR_PATH sha256sum"
    if sha256sum "$GIT_TAR_PATH" | grep "$GIT_GITHUB_HASH"; then
      echo "$GIT_TAR_PATH sha256sum = $GIT_GITHUB_HASH"
    else
      echo "sha256sum does not match"
      rm "$GIT_TAR_PATH"
    fi
  fi
fi

if [[ ! -f $GIT_TAR_PATH ]]; then
  download "${GIT_TAR_URL}" "${GIT_TAR_PATH}"
fi

if [[ -f "${GIT_TAR_PATH}" ]]; then
  cd "/tmp/buildgit" || exit 1
  tar -xvf "${GIT_TAR_PATH}"
  compilegit "/tmp/buildgit/git-$git_version" "/usr/local"
fi

echo "complete install git"

if [[ -d "/tmp/buildgit" ]]; then
  echo "delete git source files"
  rm -rf "/tmp/buildgit"
fi
